<?php
	require_once('../../lib/'.'BaseDir.inc.htm');
	require_once (EIU_LIB.'View.class.htm');
	require_once(EIU_LIB.'DBCommon.class.htm');
	require_once (EIU_CLASS.'BaseClass.htm');		
	require_once(EIU_CLASS.'IOClass.htm');
	
	$oDB = new DBCommon();
	$conn =  $oDB->stl_openConn();
	
	$modified = $_SESSION['userName'];

	// Count costcode
	$number=0;
	$IOCode="X".date("ym");
	if(isset($_POST['IOCode']) && !empty($_POST['IOCode']))
	{
		//$count = "SELECT count(`CostCode`) as count FROM `io_tbl` WHERE `CostCode` LIKE  '".$IOCode."%'";
	
		$count = "SELECT MOD(MAX(`ID`),9999) AS MaxID FROM `io_tbl` WHERE 1";
			
	}
	$rs_count = $oDB->stl_db_select($count, $conn);
	
	if(!empty($rs_count[0]->MaxID))
	{
		$number =(int)$rs_count[0]->MaxID;		
	}	
	$number=$number+1;
	
	//Create IOCode
	$IOCode = $IOCode.str_pad($number, 4, "0", STR_PAD_LEFT);
	
	//Lấy phần trăm hoa hồng mới nhất
	$newCommQuery = "SELECT `Value` FROM `commission_tbl` WHERE `isDeleted` = 0 ORDER BY `ID` DESC LIMIT 0,1";
	$rs_Comm = $oDB->stl_db_select($newCommQuery, $conn);
	$commValue = 0;
	if(!empty($rs_Comm))
	{
		$commValue =$rs_Comm[0]->Value;		
	}	
	
	//insert vao io
	$createDate=strtotime($_POST['datecreate']);	
	
	$q_insert="
		Insert into `io_tbl` 
			(
				`Type`,`CostCode`,`DeliveryReceiptDate`,
				`ReceiptPerson`,`DeliveryPerson`,`Note`,
				`isDeleted`,`CreateDate`,`LastUpdate`,
				`ModifiedBy`,`DiscountVND`,`DisPercent`,
				`SubTotalVND`,`VATAmountVND`,`TotalVND`,
				`VATPercent`,`ReceiptPersonID`,`CommissionPercent`,`CommissionAmount`,`PayCash`
			) 
		values
			(
				'".$_POST['type']."','".$IOCode."','".date("y-m-d",$createDate)."',
				'".$_POST['receipter']."','".$_POST['deliverer']."','".$_POST['note_input']."',
				0,now(),CURRENT_TIMESTAMP,
				'".$_POST['modified']."','".$_POST['disAmount']."','',
				'".$_POST['subAmount']."','".$_POST['vatAmount']."','".$_POST['totalAmount']."',
				'".$_POST['vatPercent']."','".$_POST['receipterID']."','".$commValue."','".round($commValue*$_POST['subAmount']/100)."','".$payCash."'
			)";
	//print_r($q_insert);exit();
	$rs_insert=$oDB->stl_db_change($q_insert, $conn);
	
	if(isset($_POST['info']) && !empty($_POST['info'])&&(rs_insert))
	{
		$_POST['info'] = str_replace('\"', '"',$_POST['info']);
		$io_detail = json_decode($_POST['info'],true);
		if(is_array($io_detail))
		{
			foreach($io_detail['asset'] as $key=>$value1)
			{
				$q_insert_io="
					Insert into `detail_io_tbl`
						(
							`IOCode`,`CATCode`,`AssCode`,
							`AssName`,`CATName`,`Quantifty`,
							`StatusID`,`IODescription`,`Unit`,
							`isDeleted`,`CreateDate`,`LastUpdate`,`ModifiedBy`,`Price`,`Amount`
						) 
					values
						(
							'".$IOCode."','".$value1['itemCategory']."','".$value1['itemCode']."',
							'".$value1['assName']."','".$value1['itemCategoryName']."',".$value1['quantity'].",
							'".$value1['staID']."','".$value1['despt']."','".$value1['unit']."',
							0,now(),CURRENT_TIMESTAMP,'".$modified."','".$value1['price']."','".$value1['amount']."'
						)";
				//print_r($q_insert_io);exit();
				$rs=$oDB->stl_db_change($q_insert_io, $conn);
							
			}
		}
	}
	
	echo $rs;
	$oDB->stl_closeConn($conn);
?>
